package com.qdxwx.manage.controller.my;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.upload.UploadState;
import com.qdxwx.common.upload.UploadUtils;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.manager.ManagerService;
import com.qdxwx.manage.annotation.Log;
import com.qdxwx.manage.controller.BaseController;

import com.qdxwx.manage.support.SessionUtils;
import com.qdxwx.models.enums.BusinessType;
import com.qdxwx.models.manager.Manager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;

/**
 * 个人信息 业务处理
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/my")
public class MyProfileController extends BaseController {

    @Autowired
    private ManagerService managerService;

    /**
     * 个人信息
     */
    @GetMapping("/profile.html")
    public String profile(ModelMap mmap) {
        Manager user = SessionUtils.getManager();
        mmap.put("manager", user);
        return "my/my-profile";
    }

    @GetMapping("/pwd/check.json")
    @ResponseBody
    public boolean checkPassword(String loginPwd) {
        Manager user = SessionUtils.getManager();
        return TKConvert.toPWD(loginPwd).equals(user.getLoginPwd());
    }

    @GetMapping("/pwd/reset.html")
    public String resetPwd(ModelMap mmap) {
        Manager user = SessionUtils.getManager();
        mmap.put("manager", managerService.get(user.getManagerId()));
        return "my/my-reset-pwd";
    }

    @Log(title = "重置密码", businessType = BusinessType.UPDATE)
    @PostMapping("/pwd/reset.json")
    @ResponseBody
    public AjaxResult resetPwd(String oldPassword, String newPassword) {
        Manager user = SessionUtils.getManager();
        if (TKString.isNotEmpty(newPassword)) {
            user.setLoginPwd(TKConvert.toPWD(newPassword));
            if (managerService.resetManagerPwd(user) > 0) {
                SessionUtils.setManager(managerService.get(user.getManagerId()));
                return success();
            }
            return error();
        } else {
            return error("修改密码失败，旧密码错误");
        }
    }

    /**
     * 修改头像
     */
    @GetMapping("/avatar.html")
    public String avatar(ModelMap mmap) {
        Manager user = SessionUtils.getManager();
        mmap.put("manager", managerService.get(user.getManagerId()));
        return "my/my-avatar";
    }

    /**
     * 修改用户
     */
    @Log(title = "个人信息", businessType = BusinessType.UPDATE)
    @PostMapping("/profile/update.json")
    @ResponseBody
    public AjaxResult update(Manager user) {
        Manager currentUser = SessionUtils.getManager();
        currentUser.setManagerName(user.getManagerName());
        currentUser.setMobile(user.getMobile());
        currentUser.setSex(user.getSex());
        if (managerService.update(currentUser) > 0) {
            SessionUtils.setManager(managerService.get(currentUser.getManagerId()));
            return success();
        }
        return error();
    }

    /**
     * 保存头像
     */
    @Log(title = "个人信息", businessType = BusinessType.UPDATE)
    @PostMapping("/avatar/update.json")
    @ResponseBody
    public AjaxResult updateAvatar(HttpServletRequest request) {
        Manager currentUser = SessionUtils.getManager();
        if (currentUser == null) {
            return AjaxResult.forbid();
        }
        UploadState state = UploadUtils.saveAvatarFile(request);
        if (state.getCode() == 0) {
            currentUser.setAvatar(state.getSavePath());
            if (managerService.update(currentUser) > 0) {
                SessionUtils.setManager(currentUser);
                return success();
            } else {
                return error("修改头像失败！");
            }
        } else {
            return error(state.getMsg());
        }
    }
}
